-- Rejestr przesuwny sluzacy do propagowania flagi finish

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity start_to_finish is
	generic ( delay : natural := 10 );
   port ( start, clk, rst : in std_logic;
          finish : out std_logic );
end start_to_finish;

architecture Behavioral of start_to_finish is
	signal sr: std_logic_vector((delay - 1) downto 0);
begin
	process (clk, rst)
	begin
		if (rising_edge(clk)) then
			if (rst = '1') then
				sr((delay - 2) downto 0) <= (others => '0');
				sr(delay - 1) <= '1';	-- Domyslnie oznaczenie stanu wolnego
			else
			-- Sprawdzenie czy sygnal finish nie jest ustawiony
				if (sr(delay - 1) = '1') then
					if (start = '1') then
						-- Sygnal start od poczatku
						sr((delay - 1) downto 1) <= (others => '0');
						sr(0) <=  '1';
					end if;
				else
					-- Propagacja sygnalu start
					sr((delay - 1) downto 0) <= sr((delay - 2) downto 0) & start;
				end if;
			end if;
		end if;
	end process;
	
	finish <= sr(delay - 1);
end Behavioral;
